package com.srs.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srs.model.Group;
import com.srs.model.User;
import com.srs.util.HibernateUtil;

public class UserDao {

	final static Logger logger = LoggerFactory.getLogger(UserDao.class);

	private List<User> userList;
	private User user;

	public void delete(Integer userId) {

		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			user = (User) session.get(User.class, userId);
			session.delete(user);
			tx.commit();

		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<User> list() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			userList = session.createQuery("from User").list();
			return userList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<User> listOpReceiveDailyReport() {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			userList = session.createQuery("from User where dailyReport=1").list();
			return userList;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return null;
		} finally {
			session.close();
		}

	}	
	
	@SuppressWarnings("unchecked")
	public boolean groupIsUsed(Integer groupId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			Query q = session.createQuery("from User u where u.groupId=:groupId");
			q.setInteger("groupId", groupId);
			userList = q.list();
			if (userList.size()==0){
				return false;
			}
			return true;

		} catch (Exception e) {
			System.out.print("Error while fetching " + e);
			return false;
		} finally {
			session.close();
		}

	}			
	
	public User get(Integer userId) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from User u where u.userId=:userId");
			q.setInteger("userId", userId);
			return (User) q.uniqueResult();
		} finally {
			session.close();
		}

	}

	public Boolean nameIsUsed(String userName) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from User g where g.userName=:userName");
			q.setString("userName", userName);
			userList = q.list();
			if (userList.size()==0) {
				return false;
			}
			return true;
		} finally {
			session.close();
		}

	}		

	public User login(User user) {
		Session session = HibernateUtil.getSession();
		try {
			session.beginTransaction();
			// change query for get proper data
			Query q = session.createQuery("from User u where u.userName=:userName and u.password=:password");
			q.setString("userName", user.getUserName());
			q.setString("password", user.getPassword());
			return (User) q.uniqueResult();
		} finally {
			session.close();
		}

	}
	
	
	public void insert(User user) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(user);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}

	public void update(User user) {
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(user);
			tx.commit();
		} catch (RuntimeException e) {
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			session.close();
		}

	}
	
	public static void main(String[] args) {
		// Init sessionFactory
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		HibernateUtil.setSessionFactory(sessionFactory);
		
		// Get list params
		UserDao userDao = new UserDao();
		List<User> userList = userDao.listOpReceiveDailyReport();
		System.out.println(userList);
	}
}
